print 9 + 5
print 9 - 5
print 9 / 5
print 9.0 / 5
print 9 ** 2
print 9 % 4
14 4 1 1.8 81 1
x = 2+3j
y = 5
print x + y
(7+3j)
t = -5
print abs(t)
t = 2+ 3j
print abs(t)
t = 2 + 3j
print t.real
print t.imag
5 3.60555127546 2.0 3.0
x = "Python primer" # Double quotes
print x
x = 'python primer' # Single quotes
print x
x = '"Go there", he said"' # Quotes of one kind protect the other
print x
x = """
Python primer
10 October 2015
""" # Triple quotes hold multi line strings
print x
Python primer python primer "Go there", he said" Python primer 10 October 2015
# String methods
x = "python primer"
print x.capitalize()
print x.upper()
print x.lower()
print x.center(50, "*")
print x * 2
print "%d + %d"%(5, 4) # Old style string formatting
print "{} + {}".format(5,4) # New style string formatting
print x + 2
Python primer PYTHON PRIMER python primer ******************python primer******************* python primerpython primer 5 + 4 5 + 4
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-18-a955da6437f7> in <module>() 8 print "%d + %d"%(5, 4) # Old style string formatting 9 print "{} + {}".format(5,4) # New style string formatting ---> 10 print x + 2 11 TypeError: cannot concatenate 'str' and 'int' objects
x = "python primer"
print x[0]
print x[1]
print x[0:7]
print x[0:10:2]
print x[-1]
print x[-7:-1]
print x[-1:-7:-1]
x[0] = "T" # Will error out since strings are immutable in python
p y python pto r r prime remirp
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-17-44d58066fb3a> in <module>() 8 print x[-1:-7:-1] 9 ---> 10 x[0] = "T" # Will error out since strings are immutable in python TypeError: 'str' object does not support item assignment
# Lists
x = [1, 2, 3]
print x
y = ["IEEE", "Python", 10, 10, 2015]
print y
print y[0]
y[0]= 'ieee'
print y
print y[0]
print y[-1]
y.append("Calicut")
print y
y.extend([10, 11, 12])
print y
print "Kozhikode" in y
print "Calicut" in y
[1, 2, 3] ['IEEE', 'Python', 10, 10, 2015] IEEE ['ieee', 'Python', 10, 10, 2015] ieee 2015 ['ieee', 'Python', 10, 10, 2015, 'Calicut'] ['ieee', 'Python', 10, 10, 2015, 'Calicut', 10, 11, 12] False True
# Sets - Unique elements, unordered
x = set([])
print x
x = {1, 2, 3}
print x
# print x[0] # This will error out since sets are unordered
print 5 in x
print 3 in x
x.add(10)
print x
x.add(10)
print x
x = {1,2}
y = {2,3}
print x.union(y)
print x.intersection(y)
print x - y
set([]) set([1, 2, 3]) False True set([1, 2, 3, 10]) set([1, 2, 3, 10]) set([1, 2, 3]) set([2]) set([1])
# Dictionaries
x = {"name" : "Noufal",
"class" : "python",
"place" : "Calicut"}
print x["name"]
print x["class"]
# print x["date"] # Will not work since there is no key "date"
x["strength"] = 50
print x
print x.keys()
print x.values()
print x.items()
Noufal python {'strength': 50, 'place': 'Calicut', 'name': 'Noufal', 'class': 'python'} ['strength', 'place', 'name', 'class'] [50, 'Calicut', 'Noufal', 'python'] [('strength', 50), ('place', 'Calicut'), ('name', 'Noufal'), ('class', 'python')]
print statment and string formatting.
if
for
while
def
try/except
list comprehensions
Assignment and deconstruction
Assignment semantics
x = "Python"
for i in x:
print i
print "-"*10
x = [1,2,3,4]
for i in x:
print i
for i in range(10):
print "5 x %d = %d"%(i, i*5)
P y t h o n ---------- 1 2 3 4 5 x 0 = 0 5 x 1 = 5 5 x 2 = 10 5 x 3 = 15 5 x 4 = 20 5 x 5 = 25 5 x 6 = 30 5 x 7 = 35 5 x 8 = 40 5 x 9 = 45
x = 10
if x > 5:
print "Yes, it's greater"
else:
print "Nope. It's lesser"
if x > 10:
print "Too large"
elif x < 5:
print "Too less"
else:
print "Just right"
Yes, it's greater Just right
# While loop
x = [1,2,3,4,5]
while x:
print x
x.pop()
[1, 2, 3, 4, 5] [1, 2, 3, 4] [1, 2, 3] [1, 2] [1]
# Functions
def double(x):
return x * 2
print double(10)
print double("abc")
print double([1,2,3])
# print double(dict(x=1, y=2)) # Will error out since you can't multiple a dictionary with a number
def add(first, second):
return first + second
print add(5, 6)
print add(5)
20 abcabc [1, 2, 3, 1, 2, 3] 11
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-24-8e132309a367> in <module>() 13 14 print add(5, 6) ---> 15 print add(5) TypeError: add() takes exactly 2 arguments (1 given)
# Fizz bizz
def fizzbizz(n):
for i in range(1, n+1):
if i % 15 == 0:
print "fizzbizz"
elif i % 5 == 0:
print "bizz"
elif i% 3 == 0:
print "fizz"
else:
print i
fizzbizz(20)
1 2 fizz 4 bizz fizz 7 8 fizz bizz 11 fizz 13 14 fizzbizz 16 17 fizz 19 bizz
def palindrome(s):
return s == s[::-1]
print palindrome("malayalam")
print palindrome("abba")
print palindrome("Calicut")
True True False
def panagram(s):
for i in "abcdefghijklmnopqrstuvwxyz":
if i not in s.lower():
return False
return True
print panagram("the quick brown fox jumps over the lazy dog")
print panagram("the quick brown fox jumped over the lazy dog")
True False
def freq(s):
counts = dict()
for i in s:
if i in counts:
counts[i] += 1
else:
counts[i] = 1
for i in counts:
print "{} :: {}".format(i, counts[i])
freq("she sells sea shells on the sea shore!")
a :: 2 :: 7 e :: 7 h :: 4 l :: 4 o :: 2 n :: 1 s :: 8 r :: 1 ! :: 1 t :: 1
import math
class Point(object):
def __init__(self, x, y):
self.x = x
self.y = y
def dist(self):
return math.sqrt(self.x **2 + self.y **2)
def __repr__(self):
return "Point({}, {})".format(self.x, self.y)
def __add__(self, other):
return Point(self.x + other.x, self.y + other.y)
p0 = Point(3,4)
print p0.x, p0.y
print p0.dist()
#print p0.quad() # I should get 1
#print Point(-2, 3).quad() # I should get 2
#print Point(-2, -3).quad() # I should get 3
#print Point(2, -3).quad() # I should get 4
print p0
p1 = Point(1, 2)
print p0 + p1
3 4 5.0 Point(3, 4) Point(4, 6)
class Shape(object):
def area(self):
raise NotImplementedError()
def perimeter(self):
raise NotImplementedError()
class Rectangle(Shape):
def __init__(self, l, b):
self.length = l
self.breadth = b
def area(self):
return self.length * self.breadth
def perimeter(self):
return 2*(self.length + self.breadth)
class Square(Rectangle):
def __init__(self, s):
self.side = s
super(Square, self).__init__(s, s)
s = Square(5)
print s.area()
25
Write a program to print the times tables for a given number. e.g. tables(5)
should print something like this
1 x 5 = 5 2 x 5 = 10 3 x 5 = 15 4 x 5 = 20 5 x 5 = 25 6 x 5 = 30 7 x 5 = 35 8 x 5 = 40 9 x 5 = 45 10 x 5 = 50
FizzBizz. Write a program to print numbers from 1 to a given n. For multiples of 3, print fizz
, for multiples of 5, print bizz
and for multiples of 15, print fizzbizz
.
Write a program to check if a string is an panagram
Write a program to check if a string is a palindrome
Write a program to print letter frequencies in a given string
freq("she sells sea shells on the sea shore") a :: 2 :: 7 e :: 7 h :: 4 l :: 4 o :: 2 n :: 1 s :: 8 r :: 1 t :: 1
n
using the sieve of eratosthenes.class
statementPoint
implementationdistance
methodquadrant
method__repr__
, __add__
i
, j
, k
abs(u)
)u.dot(v)
)u.cross(v)
) -(Formula : u × v = (u 2 v 3 − u 3 v 2 )i + (u 3 v 1 − u 1 v 3 )j + (u 1 v 2 − u 2 v 1 )k)